package com.tanhua.server.controller;

import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * ResponseEntity：
     *      标准响应http协议的方式，
     *      响应实体对象，spring的封装(状态码，成功或错误信息登返回)
     * 发送验证码
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ResponseEntity sendCode(@RequestBody Map<String,String> paramMap){
        // 获取手机号码
        String phone = paramMap.get("phone");
        userService.sendCode(phone);
        return ResponseEntity.ok(null);
    }

    /**
     * 登录注册功能实现
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "/loginVerification",method = RequestMethod.POST)
    public ResponseEntity login(@RequestBody Map<String,String> paramMap){
        // 手机号
        String phone = paramMap.get("phone");
        // 验证码
        String verificationCode = paramMap.get("verificationCode");
        // map ==> {"phone":"xxx","verificationCode":"xxxx"}
        Map map = userService.login(phone,verificationCode);
        return ResponseEntity.ok(map);
    }

    /**
     * 首次登录---完善资料
     * @param userInfoVo
     * @return
     */
    @RequestMapping(value = "/loginReginfo",method = RequestMethod.POST)
    public ResponseEntity loginReginfo(@RequestBody UserInfoVo userInfoVo){
        userInfoService.loginReginfo(userInfoVo);
        return ResponseEntity.ok(null);
    }

    /**
     * 完善个人信息中-上传头像
     * @param headPhoto
     */
    @RequestMapping(value = "/loginReginfo/head",method = RequestMethod.POST)
    public ResponseEntity uploadAvatar(MultipartFile headPhoto) {
        userInfoService.uploadAvatar(headPhoto);
        return ResponseEntity.ok(null);
    }
}
